package com.zg.autoflow.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xn.fastxls.InfoException;
import com.zg.autoflow.dao.mapper.WorkflowInstanceMapper;
import com.zg.autoflow.req.BackWorkflowReq;
import com.zg.autoflow.req.SettleWorkflowBackApprovalPageReq;
import com.zg.autoflow.req.SettleWorkflowBackApprovalUpdateReq;
import com.zg.autoflow.service.SettleWorkflowBackApprovalService;
import com.zg.autoflow.service.WorkflowManageService;
import com.zg.autoflow.vo.SettleWorkflowBackApprovalVo;
import com.zg.common.core.R;
import com.zg.common.core.dao.autoflow.dao.SettleWorkflowBackApprovalDao;
import com.zg.common.core.dao.autoflow.entity.SettleWorkflowBackApproval;
import com.zg.common.core.exception.BusinessException;
import com.zg.common.dds.DbTypeUtil;
import com.zg.common.web.Req;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class SettleWorkflowBackApprovalServiceImpl implements SettleWorkflowBackApprovalService {

    private final SettleWorkflowBackApprovalDao settleWorkflowBackApprovalDao;
    private final WorkflowManageService workflowManageService;
    private final WorkflowInstanceMapper workflowInstanceMapper;
    private final DbTypeUtil dbTypeUtil;

    @Override
    public R backApproval(SettleWorkflowBackApprovalUpdateReq req) {
        Long approvalId = req.getId();
        String approvalFlag = req.getApprovalStatus();
        if (ObjectUtils.isEmpty(approvalId) || ObjectUtils.isEmpty(approvalFlag)) {
            throw new BusinessException(500,"缺少必要入参");
        }
        SettleWorkflowBackApproval approval = settleWorkflowBackApprovalDao.getById(approvalId);
        if (ObjectUtils.isEmpty(approval)) {
            throw new InfoException("未查询到该退回审批单");
        }

        if (!"0".equals(approval.getApprovalStatus())) {
            throw new InfoException("该退回审批单已审批完成");
        }
        //1.同意  2.拒绝
        if ("1".equals(approvalFlag)) {
            BackWorkflowReq backWorkflowReq=new BackWorkflowReq();
            backWorkflowReq.setCurStepInstCode(approval.getCurrStepInstCode());
            backWorkflowReq.setCurInstCode(approval.getInstCode());
            backWorkflowReq.setBackStepInstCodes(approval.getBackStepInstCode());
            backWorkflowReq.setBackDescs( approval.getBackReason());
            backWorkflowReq.setApprovalFlag( "1");
            backWorkflowReq.setBackUserId( approval.getCurrStepUser());
            workflowManageService.backWorkflow(backWorkflowReq);
            approval.setApprovalStatus("1");
        }
        if ("2".equals(approvalFlag)) {
            approval.setApprovalStatus("2");
        }
        settleWorkflowBackApprovalDao.updateById(approval);
        return R.success();
    }

    @Override
    public R<IPage<SettleWorkflowBackApprovalVo>> getBackApproval(SettleWorkflowBackApprovalPageReq req) {
        req.setUserId(Req.getCurrUser().getId());
        String dbType =null;
        if ( dbTypeUtil.isMysql()){
            dbType="0";
        }else if (dbTypeUtil.isOracle()){
            dbType="1";
        }
        req.setDbType(dbType);
        IPage<SettleWorkflowBackApprovalVo> result =workflowInstanceMapper.getBackApprovalPage(new Page(req.getPage(), req.getPageSize()),req);
        result.getRecords().forEach(line->{
            if ("0".equals(line.getApprovalStatus())){
                line.setApprovalStatusName("待审批");
            }else if ("1".equals(line.getApprovalStatus())){
                line.setApprovalStatusName("已通过");
            }else if ("2".equals(line.getApprovalStatus())){
                line.setApprovalStatusName("未通过");
            }
        });
        return R.success(result);
    }
}
